var app = getApp()

Page({
  data: {
    uid: "",
    info: [],
    details: {},
    isShowDetails: false,
    controls: [],
    isAuth: true
  },
  onLoad: function (options) {
    const that = this
    // 查看是否授权
    wx.getSetting({

      success: function (res) {

        if (res.authSetting['scope.userInfo']) {

          wx.checkSession({
            success: function () {

              console.log("未过期，并且在本生命周期一直有效");

              var uid = wx.getStorageSync('uid');

              if (uid) {
                app.getUserInfoFromServer(uid, function (userinfo) {

                  console.log("tt" + app.globalData.openid);

                });
              } else {
                //没有授权 默认授权
                that.setData({
                  isAuth: false
                });
              }
            },
            fail: function () {
              console.log("已经失效，需要重新执行登录流程");

              var uid = wx.getStorageSync('uid');

              if (uid) {

                app.getUserInfoLogin(uid, function (userinfo) {

                  console.log(app.globalData.openid);
                });
              } else {
                //没有授权 默认授权
                that.setData({
                  isAuth: false
                });
              }
            }
          })

        } else {
          that.setData({
            isAuth: false
          });
          //没有授权 默认授权
        }
      }
    })

  },

  /**
   * session_key未过期，直接从服务器获取用户信息
   */
  getUserInfoFromServer: function (uid, cb) {

    wx.request({
      url: app.globalData.website + 'webchat/info',
      data: {
        userId: uid
      },
      success: function (res) {

        if (res.data.message == "成功") {

          var userinfo = res.data.data;

          typeof cb == "function" && cb(userinfo);
        }
      }
    })
  },
  /**
   * 下载图片
   */

  saveImg: function () {
    wx.downloadFile({
      url: '../../images/qrcode.jpg',
      success: function (res) {
        console.log(res)
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function (res) {
            console.log(res)
          },
          fail: function (res) {
            console.log(res)
            console.log('fail')
          }
        })
      },
      fail: function () {
        console.log('fail')
      }
    })

  },


  /**
   * 提交
   */
  formSubmit: function (e) {
    if (e.detail.value.phone === '') {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'success',
        duration: 2500
      })
      return;
    }
  
    if (e.detail.value.content === '') {
      wx.showToast({
        title: '留言不能为空',
        icon: 'success',
        duration: 2500
      })
      return;
    }

    this.getUserInfoFromServer()

    wx.request({

      url: app.globalData.website + 'Leaves/insertLeaves',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      data: {
        content: e.detail.value.content,
        phone: e.detail.value.phone,
        openid: app.globalData.openid,
        name: e.detail.value.reward
      },
      success: function (res) {
        wx.showToast({
          title: '提交成功',
        })
        that.setData({
          phone: '',
          formdata: '',
          reward: ''
        })
      }
    })
  }





  
})